
clear all
set more off
capture log close
est clear


global figureA1 		= 0 // 1: run, 0: not 
global figureA2			= 0
global figureA3			= 0
global figureA4			= 0
 
global figureB1			= 0 
global figureB2			= 0 



*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Figure A1 *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $figureA1 == 1 {


use "data/working/working_coinmarketcap.dta", replace 

keep if date >= date("20170101","YMD")
keep if currency == "bitcoin"

corr close volume

twoway 	(line close date , sort ) ///
		(line volume date , sort lpattern(dash) yaxis(2)), ///
		ytitle("Bitcoin price ($)") ytitle("Bitcoin volume (M$)", axis(2)) xtitle("") /// 
		legend(label(1 "Price") label(2 "Volume") r(1)) ///
		text(15000 20900 "Correlation" "price-volume: 0.89", place(e)) ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figureA1a.pdf", replace	


gen month=month(date)
gen year=year(date)
gen ym_date =ym(year,month)
format ym_date %tm


collapse (mean) close (sum) volume, by(ym_date)

tset ym_date
gen close_deltapct 	= (close - l.close) / close * 100
gen volume_deltapct = (volume - l.volume) / volume * 100


corr close_deltapct volume_deltapct

twoway 	(connect close_deltapct ym_date , sort msymbol(O)) ///
		(connect volume_deltapct ym_date , sort lpattern(dash) msymbol(X)), ///
		ytitle("Bitcoin price ($)") xtitle("") /// 
		legend(label(1 "Price") label(2 "Volume") r(1)) ///
		text(70 700 "Correlation" "{&Delta}price-{&Delta}volume: 0.68", place(e)) ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figureA1b.pdf", replace	


}

*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Figure A2 *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $figureA2 == 1 {
	
use "data/working/working_coinmarketcap.dta", replace 
	
keep if inlist(currency, "bitcoin", "bitcoin-cash", "dash", "ethereum") ///
		| inlist(currency, "litecoin", "monero", "ripple", "zcash")

		
* keep only 2017 data		
*gen date2=date(date,"YMD")
*format date2 %td
keep if inrange(date , td(01sep2017), td(01oct2018))

*normalize first open = 100
sort currency date
bysort currency: gen open_norm = 100 * open/open[1]

*generate currency_id
egen currency_id = group(currency)

*generate date variable
gen month = mofd(date)
format month %tm

*label currency_id
label define currencylabel 1 "bitcoin" 2 "bitcoin-cash" 3 "dash" ///
		4 "ethereum" 5 "litecoin" 6 "monero" 7 "ripple" 8 "zcash" 
label values currency_id currencylabel
label variable open_norm "Adjusted open price"
label variable date "Date"

*set up panel data
xtset currency_id date

xtline open_norm, overlay ///
		ytitle("Normalized open price (Sep 2017 = 100)") ysc(titlegap(3)) xtitle("") /// 
		legend(r(2)) xlabel(,labsize(small) angle(45)) ///
		plot1opts(lwidth(thick)) ///
		plot2opts(lwidth(thin)) plot3opts(lwidth(thin)) plot4opts(lwidth(thin)) plot5opts(lwidth(thin))  ///
		plot6opts(lwidth(thin)) plot7opts(lwidth(thin))  ///
		xline(21193, lwidth(5) lc(gs12)) ///
		xline(21373, lwidth(5) lc(gs12)) ///
		ttext(1500 21193 "Wave 1", size(vsmall)) ///
		ttext(1500 21373 "Wave 2", size(vsmall)) ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figureA2.pdf", replace		

	
}	

*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Figure A3 *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $figureA3 == 1 {


use "data/working/working_coinmarketcap.dta", replace 

keep if currency == "bitcoin"

merge 1:1 date using "data/working/working_blockchain_uniqueaddresses.dta"
keep if _merge!=2

keep if date >= date("20170101","YMD")

replace num_uni = num_uni /1000

twoway 	(line close date , sort color(navy) ) ///
		(line num_uni date , sort lpattern(dash) yaxis(2) color(dkgreen)) , ///
		ytitle("Bitcoin price ($)") ytitle("Number of Unique Addresses (.000)", axis(2)) xtitle("") /// 
		legend(label(1 "Price") label(2 "Number of Unique Addresses") r(1)) ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figureA3.pdf", replace	


}


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Figure A4 *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $figureA4 == 1 {

use "data/working/data_platform.dta", clear

*------------------------------------------------------------------------------*
* Variables *
*------------------------------------------------------------------------------*

sort currency_id
encode currency_id, gen(currency_id_num)

unique household_id currency_id_num

* Across individuals 
preserve
xtset household_id currency_id_num
pwcorrf currencies_potential, reshape //  help pwcorrf
matrix list r(C), format(%4.2f)
matrix C = r(C)

matrix D = J(100,100,0) // initialize new matrix to desired dimensions
mat list D

forval i=1/100 { 
	forval j=1/100 {
		mat D[`i',`j'] = C[`i',`j'] // arbitrary choice of destination
	}
}
mat list D

heatplot D, legend(off) aspectratio(0.8) lower drop(0) /// 
		color(hcl, diverging intensity(1)) /// 
		xlabel(, nolabels) ylabel(, nolabels) ///
		graphregion(color(white))  //
graph export "output/figures/figureA4b.pdf", replace	

xtcdf currencies_potential // Test strongly indicates currency potential is correlated across individuals
restore
	
	
* Across currencies_potential
preserve
bys currency_id_num: egen tmp = seq()
xtset currency_id_num tmp 

pwcorrf currencies_potential, reshape //  help pwcorrf
matrix list r(C), format(%4.2f)
matrix C = r(C)
heatplot C, values(format(%9.2f) mlabsize(vsmall) ) legend(off) aspectratio(0.8) lower xlabel(, angle(45)) /// 
		cuts(-0.05 0 0.05 0.99) color(hcl, diverging intensity(.5)) /// 
		xlabel(1 "Alt" 2 "Bitcoin" 3 "Bitcoin-cash" 4 "Dash" 5 "Ethereum"  6 "Litecoin"  7 "Monero"  8 "Ripple" 9 "Zcash", labsize(small) ) ///
		ylabel(1 "Alt" 2 "Bitcoin" 3 "Bitcoin-cash" 4 "Dash" 5 "Ethereum"  6 "Litecoin"  7 "Monero"  8 "Ripple" 9 "Zcash", labsize(small) ) ///
		graphregion(color(white))  //
graph export "output/figures/figureA4a.pdf", replace	

xtcdf currencies_potential // Test strongly indicates currency potential is correlated across currencies
restore

}


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Figure B1 *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $figureB1 == 1 {
	
use "data/working/working_coinmarketcap.dta", replace 

rename supply supply_check

keep if currency == "bitcoin"

merge 1:1 date using "data/working/1. bitcoin.dta" 
keep if _merge==3

twoway 	(line close date , sort color(navy)) ///
		(line supply date , sort lpattern(dash) yaxis(2) color(maroon)), ///
		ytitle("Bitcoin price ($)") ytitle("Bitcoin in circulation (M)", axis(2)) xtitle("") /// 
		legend(label(1 "Price") label(2 "Supply") r(1)) ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figureB1.pdf", replace	

	
}	


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Figure B2 *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $figureB2 == 1 {
	
import delimited "data/source/BTC_ETH_Price.csv", clear varnames(1)
	
generate date2 = date(date, "MDY", 2025)
format %td date2
drop date
rename date2 date
	

	
foreach var in close_btc close_eth  { // 

* Announcement: Sep 7, 2018 OR Apr 30, 2018 
	gen `var'_norm_announcement1 = `var' if date == mdy(4,30,2018) // 
	sort `var'_norm_announcement1
	replace `var'_norm_announcement1 = `var'_norm_announcement1[1] if missing(`var'_norm_announcement1)
	replace `var'_norm_announcement1 = `var'/`var'_norm_announcement1 * 100			

	gen `var'_norm_announcement2 = `var' if date == mdy(9,7,2018) // 
	sort `var'_norm_announcement2
	replace `var'_norm_announcement2 = `var'_norm_announcement2[1] if missing(`var'_norm_announcement2)
	replace `var'_norm_announcement2 = `var'/`var'_norm_announcement2 * 100			
	
* Implementation: September 15, 2022  		
	gen `var'_norm_implementation = `var' if date == mdy(9,14,2022) // 
	sort `var'_norm_implementation 
	replace `var'_norm_implementation = `var'_norm_implementation[1] if missing(`var'_norm_implementation)
	replace `var'_norm_implementation = `var'/`var'_norm_implementation * 100			
	
}
	
twoway 	(line close_eth_norm_announcement1 	date if date >= mdy(3,30,2018) & date<= mdy(5,30,2018) , sort) ///
		(line close_btc_norm_announcement1  date if date >= mdy(3,30,2018) & date<= mdy(5,30,2018) , sort lpattern(dash)), ///
		ytitle("Price (announcement date = 100)")  xtitle("") /// 
		legend(label(1 "ETH") label(2 "BTC") r(2) ring(0) position(2)) ///
		tline(30apr2018, lcolor(black) lpattern(dash)) ///
		ylabel(60(20)120) ///
		graphregion(color(white)) bgcolor(white) //
graph export "output/figures/figureB2b.pdf", replace	
	
twoway 	(line close_eth_norm_implementation  date if date >= mdy(8,14,2022) & date<= mdy(10,14,2022) , sort) ///
		(line close_btc_norm_implementation  date if date >= mdy(8,14,2022) & date<= mdy(10,14,2022) , sort lpattern(dash)), ///
		ytitle("Price (implementation date = 100)")  xtitle("") /// 
		legend(label(1 "ETH") label(2 "BTC") r(2) ring(0) position(2)) ///
		tline(14sep2022, lcolor(black) lpattern(dash)) ///
		ylabel(60(20)120) ///
		graphregion(color(white)) bgcolor(white) //
graph export "output/figures/figureB2a.pdf", replace	
	
	
}


	
	
